﻿using System.Threading.Tasks;
using Aierp.Entities;
using Aierp.Repository.Implements;
using Aierp.Utility;
using Aierp.Utility.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace Aierp.Controllers
{
    /// <summary>
    /// 后台注册用户管理
    /// </summary>
    [Route("api/[controller]")]
    public class CustomerManageController : ControllerBase
    {

        CustomersRepository Repository { get; set; }
        SysVisitor SysVisitor { get; set; }
        public CustomerManageController(CustomersRepository repository, SysVisitor sysVisitor)
        {
            Repository = repository;
            SysVisitor = sysVisitor;
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/values/5
        [HttpGet("{id}")]
        public async Task Get(int id)
        {
            var model = Repository.FindById(id);
            if (model == null)
            {
                await WriteFailJson();
                return;
            }
            await WriteResultJson(model);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetList")]
        public async Task GetList()
        {
            string modelName = HttpContext.Request.Query["modelName"];
            var pageParam = this.GetPageParam();
            await Response.WriteAsync(Repository.GetList(pageParam.Order, pageParam.Limit, pageParam.Offset, modelName));
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <returns></returns>
        [HttpPost("Update")]
        public async Task Update()
        {
            //获取用户对象
            var model = GetModel<C1_Customer>();

            if (model.UserName.IsEmpty() || model.CustName.IsEmpty())
            {
                await this.WriteSpecReasonJson("保存失败，错误信息如下：用户名或真实姓名不能为空！");
                return;
            }

            if (Repository.Update(model, SysVisitor.UserName) > 0)
            {
                await this.WriteSucessJson();
            }
            else
            {
                await this.WriteFailJson();
            }
        }

        /// <summary>
        /// 停用用户
        /// </summary>
        /// <param name="id"></param>
        // DELETE api/values/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            if (id == 0)
            {
                await this.WriteSpecReasonJson("系统中没有这个用户！");
                return;
            }

            var result = Repository.Remove(id, SysVisitor);
            if (result > 0)
            {
                await this.WriteSucessJson();
            }
            else switch (result)
                {
                    case -2:
                        await this.WriteSpecReasonJson("系统中找不到这个用户！");
                        break;
                    case -9:
                        await this.WriteSpecReasonJson("admin账户不能删除！");
                        break;
                }
        }


        /// <summary>
        /// 重置密码
        /// </summary>
        [HttpPost("ResetPassWord")]
        public async Task ResetPassWord()
        {
            int id;

            int.TryParse(HttpContext.Request.Form["KeyId"], out id);
            if (id == 0)
            {
                await this.WriteSpecReasonJson("系统中没有这个用户！");
                return;
            }

            var result = Repository.ResetPassWord(id);
            if (result > 0)
            {
                await this.WriteSucessJson();
            }
            else if (result == -2)
            {
                await this.WriteSpecReasonJson("系统中找不到这个用户！");
            }
            else if (result <= 0)
            {
                await this.WriteSpecReasonJson("重置失败，请使用初始密码登录，或联系管理员！");
            }
        }

    }
}
